﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using DotNetNukeMVPWizard.ProjectConfiguration;

namespace DotNetNukeMVPWizard
{
    internal partial class ProjectConfigurationForm : Form, IConfigurationForm
    {
        #region Fields / Constants
        private const string ERROR_MESSAGE_FORMAT = "The value '{0}' is not valid for a {1}. {2}";
        #endregion

        #region Constructor
        public ProjectConfigurationForm()
        {
            InitializeComponent();
        }
        #endregion

        #region Event Handlers
        private void ProjectConfigurationForm_Load(object sender, EventArgs e)
        {
            txtYourCompany.Text = this.Configuration.YourCompany;
            txtProjectNamespaceName.Text = this.Configuration.ProjectNamespaceName;
            txtIisAppRootUrl.Text = this.Configuration.IisAppRootUrl;
            txtPathIisAppRootUrl.Text = this.Configuration.PathIisAppRootUrl;
            txtYourCompany.Focus();

            Dictionary<string, string> placeholders = new Dictionary<string, string>();
            Configuration.FillDictionary(placeholders);

            this.DataGridCustomProperties.DataSource = placeholders.ToArray();
        }

        private void btnBrowsePathToIISAppRoot_Click(object sender, EventArgs e)
        {
            FolderBrowser = new FolderBrowserDialog();
            if (FolderBrowser.ShowDialog() == DialogResult.OK)
            {
                this.Configuration.SetPathIisAppRootUrl(FolderBrowser.SelectedPath);
                txtPathIisAppRootUrl.Text = this.Configuration.PathIisAppRootUrl;
            }
            FolderBrowser = null;
        }

        private void ConfigureProject_Click(object sender, EventArgs e)
        {
            if (Configuration.IsValid)
            {
                DialogResult = DialogResult.OK;
                this.Dispose();
            }
            else
            {
                MessageBox.Show("There are errors with the project configuration settings. Please correct the error before clicking \"Configure Project\".\n" + Configuration.ErrorMessages, "Placeholder Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                
                FormValidator.SetError(ConfigureProject, "There are errors with the project configuration settings. Please correct the error before clicking \"Configure Project\"" + Configuration.ErrorMessages);
            }
        }
        #endregion

        #region IConfigurationForm Implementations
        DialogResult IConfigurationForm.ShowDialog()
        {
            return base.ShowDialog();
        }

        public IMvpMultiProjectConfiguration Configuration { get; set; }
        #endregion

        #region Event Handlers - User Input Validation
        private void txtProjectNamespaceName_Validating(object sender, CancelEventArgs e)
        {
            string originalValue = Configuration.ProjectNamespaceName;
            string text = txtProjectNamespaceName.Text;
            string tag = txtProjectNamespaceName.Tag.ToString();
            const string STR_LabelText = "Project Namespace";
            Configuration.SetProjectNamespaceName(text);

            if (! Configuration.IsValidProjectNamespaceName)
            {
                string errorMsg = string.Format(ERROR_MESSAGE_FORMAT, text, STR_LabelText, tag);
                this.ProjectNamespaceNameValidator.SetError(sender as TextBox, errorMsg);
            }
            else if (! Configuration.ProjectNamespaceName.Equals(text))
            {
                txtProjectNamespaceName.Text = Configuration.ProjectNamespaceName;
                this.ProjectNamespaceNameValidator.SetError(sender as TextBox, String.Format("The value '{0}' that you entered was transformed to a valid safe identifier.", text));
            }
            else
            {
                txtProjectNamespaceName.Text = Configuration.ProjectNamespaceName;
                this.ProjectNamespaceNameValidator.SetError(sender as TextBox, string.Empty);
            }
        }

        private void txtYourCompany_Validating(object sender, CancelEventArgs e)
        {
            string text = txtYourCompany.Text;
            string tag = txtYourCompany.Tag.ToString();
            const string STR_LabelText = "Project Owner";
            Configuration.SetYourCompany(text);

            if (!Configuration.IsValidYourCompany)
            {
                string errorMsg = string.Format(ERROR_MESSAGE_FORMAT, text, STR_LabelText, tag);
                this.YourCompanyValidator.SetError(sender as TextBox, errorMsg);
            }
            else
            {
                this.YourCompanyValidator.SetError(sender as TextBox, string.Empty);
            }
        }

        private void txtPathIisAppRootUrl_Validating(object sender, CancelEventArgs e)
        {
            string text = txtPathIisAppRootUrl.Text;
            string tag = txtPathIisAppRootUrl.Tag.ToString();
            const string STR_LabelText = "File Path to DotNetNuke Application";
            Configuration.SetPathIisAppRootUrl(text);

            if (!Configuration.IsValidPathIisAppRootUrl)
            {
                string errorMsg = string.Format(ERROR_MESSAGE_FORMAT, text, STR_LabelText, tag);
                this.PathIisAppRootUrlValidator.SetError(sender as TextBox, errorMsg);
            }
            else
            {
                txtPathIisAppRootUrl.Text = Configuration.PathIisAppRootUrl;
                this.PathIisAppRootUrlValidator.SetError(sender as TextBox, string.Empty);
            }
        }

        private void txtIisAppRootUrl_Validating(object sender, CancelEventArgs e)
        {
            string text = txtIisAppRootUrl.Text;
            string tag = txtIisAppRootUrl.Tag.ToString();
            const string STR_LabelText = "Local DotNetNuke Website Url";
            Configuration.SetIisAppRootUrl(text);

            Cursor.Current = Cursors.WaitCursor; // Note: it might take a few seconds to check the URL
            if (!Configuration.IsValidIisAppRootUrl)
            {
                string errorMsg = string.Format(ERROR_MESSAGE_FORMAT, text, STR_LabelText, tag);
                this.IisAppRootUrlValidator.SetError(sender as TextBox, errorMsg);
            }
            else
            {
                txtIisAppRootUrl.Text = Configuration.IisAppRootUrl;
                this.IisAppRootUrlValidator.SetError(sender as TextBox, string.Empty);
            }
            Cursor.Current = DefaultCursor;
        }
        #endregion
    }
}
